import matplotlib.pyplot as plt
import numpy as np

import os
path = os.path.dirname(__file__) # 先找到当前文件 a.py 所在的目录
file_name1 = os.path.join(path,'C_2.txt') # 拼接文件的路径
file_name2 = os.path.join(path,'C_3.txt')
file_name3 = os.path.join(path,'C_1.txt')
a = []
b = []
c = []
file1 = open(file_name1, mode = 'r')
for line in file1:
    line = line.split()
    a.append(line)
file1.close()

file2 = open(file_name2, mode = 'r')
for line in file2:
    line = line.split()
    b.append(line)
file2.close()

file3 = open(file_name3, mode = 'r')
for line in file3:
    line = line.split()
    c.append(line)
file3.close()

a1 = []
for m in a:
    a1.append(float(m[0]))
a = a1
b1 = []
for m in b:
    b1.append(float(m[0]))
b = b1
c1 = []
for m in c:
    c1.append(float(m[0]))
c = c1


n3 = 1000
n2 = 900
x2 = np.linspace(-4.5,4.5,n2)
x3 = np.linspace(-5,5,n3)
y = 1/(1+x3*x3)


#plt.figure(1)
Y2=np.zeros((n2,1))
Y3=np.zeros((n3,1))
Y1=np.zeros((n2,1))
for i in range(0,n2):
    Y2[i][0] = a[i]
for i in range(0,n3):
    Y3[i][0] = b[i]
for i in range(0,n2):
    Y1[i][0] = c[i]
#Y21=np.array(Y2)
plt.plot(x3,y)
plt.plot(x2,Y2[:,0])
plt.plot(x3,Y3[:,0])
plt.plot(x2,Y1[:,0])
plt.legend(['f','order=2','order=3','order=1'])
plt.savefig(os.path.join(path,"figure/C.png"))


